{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from pandas import Series, DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>tpep_pickup_datetime</th>\n",
       "      <th>tpep_dropoff_datetime</th>\n",
       "      <th>passenger_count</th>\n",
       "      <th>trip_distance</th>\n",
       "      <th>total_amount</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2019-07-01 00:51:04</td>\n",
       "      <td>2019-07-01 00:51:33</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>4.94</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2019-07-01 00:46:04</td>\n",
       "      <td>2019-07-01 01:05:46</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4.16</td>\n",
       "      <td>20.30</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2019-07-01 00:25:09</td>\n",
       "      <td>2019-07-01 01:00:56</td>\n",
       "      <td>1.0</td>\n",
       "      <td>18.80</td>\n",
       "      <td>70.67</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2019-07-01 00:33:32</td>\n",
       "      <td>2019-07-01 01:15:27</td>\n",
       "      <td>1.0</td>\n",
       "      <td>18.46</td>\n",
       "      <td>66.36</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2019-07-01 00:00:55</td>\n",
       "      <td>2019-07-01 00:13:05</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.70</td>\n",
       "      <td>15.30</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  tpep_pickup_datetime tpep_dropoff_datetime  passenger_count  trip_distance  \\\n",
       "0  2019-07-01 00:51:04   2019-07-01 00:51:33              1.0           0.00   \n",
       "1  2019-07-01 00:46:04   2019-07-01 01:05:46              1.0           4.16   \n",
       "2  2019-07-01 00:25:09   2019-07-01 01:00:56              1.0          18.80   \n",
       "3  2019-07-01 00:33:32   2019-07-01 01:15:27              1.0          18.46   \n",
       "4  2019-07-01 00:00:55   2019-07-01 00:13:05              0.0           1.70   \n",
       "\n",
       "   total_amount  \n",
       "0          4.94  \n",
       "1         20.30  \n",
       "2         70.67  \n",
       "3         66.36  \n",
       "4         15.30  "
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "filename = '../data/nyc_taxi_2019-07.csv'\n",
    "\n",
    "df = (\n",
    "    pd\n",
    "    .read_csv(filename,\n",
    "              usecols=['tpep_pickup_datetime',\n",
    "                       'tpep_dropoff_datetime',\n",
    "                       'trip_distance', \n",
    "                       'passenger_count',\n",
    "                       'total_amount'],\n",
    "              parse_dates=['tpep_pickup_datetime', \n",
    "                           'tpep_dropoff_datetime'])\n",
    ")\n",
    "\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Beyond 1\n",
    "\n",
    "The data set that we loaded is supposed to be for July 2019. How many trips are not from July 2019?  That is, how many records are in the wrong file?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "387"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Remember that without a time, it defaults to midnight!\n",
    "\n",
    "len(df[(df['tpep_pickup_datetime'] < '2019-07-01') |\n",
    "   (df['tpep_pickup_datetime'] > '2019-07-31 23:59' )].index)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Beyond 2\n",
    "\n",
    "What was the mean trip time for each number of passengers?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "passenger_count\n",
       "0.0   0 days 00:14:18.929810752\n",
       "1.0   0 days 00:17:46.148103924\n",
       "2.0   0 days 00:18:34.024342704\n",
       "3.0   0 days 00:19:02.079604271\n",
       "4.0   0 days 00:20:10.057290100\n",
       "5.0   0 days 00:22:29.870464324\n",
       "6.0   0 days 00:20:54.109564300\n",
       "7.0   0 days 00:16:38.206896551\n",
       "8.0      0 days 00:11:00.500000\n",
       "9.0      0 days 00:49:16.125000\n",
       "Name: trip_time, dtype: timedelta64[ns]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['trip_time'] = df['tpep_dropoff_datetime'] - df['tpep_pickup_datetime']\n",
    "df.groupby('passenger_count')['trip_time'].mean()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Beyond 3\n",
    "\n",
    "Load taxi data from July 2019 and 2020. For each year, and then for each number of passengers, what was the mean amount paid?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>tpep_pickup_datetime</th>\n",
       "      <th>tpep_dropoff_datetime</th>\n",
       "      <th>passenger_count</th>\n",
       "      <th>trip_distance</th>\n",
       "      <th>total_amount</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2019-07-01 00:51:04</td>\n",
       "      <td>2019-07-01 00:51:33</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>4.94</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2019-07-01 00:46:04</td>\n",
       "      <td>2019-07-01 01:05:46</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4.16</td>\n",
       "      <td>20.30</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2019-07-01 00:25:09</td>\n",
       "      <td>2019-07-01 01:00:56</td>\n",
       "      <td>1.0</td>\n",
       "      <td>18.80</td>\n",
       "      <td>70.67</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2019-07-01 00:33:32</td>\n",
       "      <td>2019-07-01 01:15:27</td>\n",
       "      <td>1.0</td>\n",
       "      <td>18.46</td>\n",
       "      <td>66.36</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2019-07-01 00:00:55</td>\n",
       "      <td>2019-07-01 00:13:05</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.70</td>\n",
       "      <td>15.30</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  tpep_pickup_datetime tpep_dropoff_datetime  passenger_count  trip_distance  \\\n",
       "0  2019-07-01 00:51:04   2019-07-01 00:51:33              1.0           0.00   \n",
       "1  2019-07-01 00:46:04   2019-07-01 01:05:46              1.0           4.16   \n",
       "2  2019-07-01 00:25:09   2019-07-01 01:00:56              1.0          18.80   \n",
       "3  2019-07-01 00:33:32   2019-07-01 01:15:27              1.0          18.46   \n",
       "4  2019-07-01 00:00:55   2019-07-01 00:13:05              0.0           1.70   \n",
       "\n",
       "   total_amount  \n",
       "0          4.94  \n",
       "1         20.30  \n",
       "2         70.67  \n",
       "3         66.36  \n",
       "4         15.30  "
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "all_filenames = ['../data/nyc_taxi_2019-07.csv',\n",
    "                '../data/nyc_taxi_2020-07.csv']\n",
    "\n",
    "all_dfs = [pd.read_csv(one_filename,\n",
    "                usecols=['tpep_pickup_datetime',\n",
    "                        'tpep_dropoff_datetime',\n",
    "                         'trip_distance', 'passenger_count', 'total_amount'],\n",
    "                 parse_dates=['tpep_pickup_datetime', 'tpep_dropoff_datetime'])\n",
    "           for one_filename in all_filenames]\n",
    "\n",
    "df = pd.concat(all_dfs)\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tpep_pickup_datetime  passenger_count\n",
       "2002                  1.0                18.002500\n",
       "                      2.0                18.800000\n",
       "2008                  1.0                18.340000\n",
       "                      2.0                42.860000\n",
       "                      5.0                11.966667\n",
       "2009                  1.0                23.923571\n",
       "                      2.0                45.316000\n",
       "2010                  2.0                18.360000\n",
       "2019                  0.0                18.981793\n",
       "                      1.0                19.284646\n",
       "                      2.0                20.097442\n",
       "                      3.0                20.208111\n",
       "                      4.0                21.063172\n",
       "                      5.0                19.419311\n",
       "                      6.0                19.386516\n",
       "                      7.0                70.080690\n",
       "                      8.0                74.760455\n",
       "                      9.0                93.509375\n",
       "2020                  0.0                16.538912\n",
       "                      1.0                16.856554\n",
       "                      2.0                17.188322\n",
       "                      3.0                17.103106\n",
       "                      4.0                17.964939\n",
       "                      5.0                16.725836\n",
       "                      6.0                16.812911\n",
       "                      7.0                22.456000\n",
       "                      8.0                10.300000\n",
       "                      9.0                11.760000\n",
       "Name: total_amount, dtype: float64"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.groupby([df['tpep_pickup_datetime'].dt.year, 'passenger_count'])['total_amount'].mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
